import {
  defineStore
} from 'pinia';
import {
  ref
} from 'vue';
export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')
    const openId = ref('')
    const userInfo = ref(null)
    const setData = (val = '', key = 'token') => {
      switch (key) {
        case 'openId':
          openId.value = val
          break;
        case 'userInfo':
          userInfo.value = val
          break;
        default:
          token.value = val
          break;
      }
    }
    return {
      token,
      openId,
      userInfo,
      setData
    }
  },
  //pinia持久化，必须在使用持久化插件后才能设置
  {
    // persist: true 是用来给网页端持久化的
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          return uni.setStorageSync(key, value)
        }
      }
    }
  }
)